//
// Created by Martin on 2023/4/19.
//

#include <iostream>
#include <thread>
#include <fstream>
#include <memory>
#include <vector>

#include "ftpserver/UserDao.h"
#include "ftpserver/UserInfo.h"

#define BOOST_TEST_MODULE UserDaoTest
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>

using namespace std;
using namespace ftp;

BOOST_AUTO_TEST_CASE(testUserDaoAddUser)
{
    UserDao userDao;
    const std::string username = "user";
    const std::string password = "123";
    userDao.addUser(username, password, "/", Permission::All);
    auto user = userDao.getUserInfo("user", "123");

    BOOST_CHECK_EQUAL(username, user->username_);
    BOOST_CHECK_EQUAL(password, user->password_);
}

BOOST_AUTO_TEST_CASE(testUserDaoAnonymousUser)
{
    UserDao userDao;
    const std::string anonymous = "anonymous";

    // add anonymous user for test
    userDao.addUser(anonymous, "", "/", Permission::ReadOnly);

    userDao.enableAnonymousUser(); // default settings
    {
        auto user = userDao.getUserInfo("anonymous", "");
        BOOST_CHECK_EQUAL(anonymous, user->username_);
    }

    {
        auto user = userDao.getUserInfo("", "");
        BOOST_CHECK_EQUAL(anonymous, user->username_);
    }

    userDao.disableAnonymousUser();
    {
        auto user = userDao.getUserInfo("anonymous", "");
        BOOST_CHECK_EQUAL(user, nullptr);
    }

    {
        auto user = userDao.getUserInfo("", "");
        BOOST_CHECK_EQUAL(user, nullptr);
    }
}
